package com.peaksport.framework.extend.swagger;

import javax.ws.rs.core.Response.Status;

import org.apache.servicecomb.swagger.invocation.Response;
import org.apache.servicecomb.swagger.invocation.SwaggerInvocation;
import org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter;
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;

import com.peaksport.framework.exception.PKDevException;
import com.peaksport.framework.util.PKPublicUtils;
import com.peaksport.framework.vo.JSONResultSet;

import lombok.extern.slf4j.Slf4j;
/**
 * 
 *  PKBizException异常封装转换类，采用的java SPI机制注册
 * 
 *
 * @author jinbin_guo 2019年4月20日
 *
 */
@Slf4j
public class PKDevExceptionToResponseConverter implements ExceptionToProducerResponseConverter<PKDevException> {

	@Override
	public Class<PKDevException> getExceptionClass() {
		return PKDevException.class;
	}

	@Override
	public Response convert(SwaggerInvocation swaggerInvocation, PKDevException e) {
		JSONResultSet rs = new JSONResultSet();
		rs.setResultCode(98);
		rs.setResultMsg(e.getMessage());
		String detailExpMsg = PKPublicUtils.getStackTrace(e);
		detailExpMsg = detailExpMsg.replace("\r\n\t", "<br>");
		detailExpMsg = detailExpMsg.replace("\r\n", "<br>");
		rs.setDetailExpMsg(detailExpMsg);
		if (log.isInfoEnabled())
			log.error(null, e);
		e.printStackTrace();
	    InvocationException state = new InvocationException(Status.INTERNAL_SERVER_ERROR, rs);
	    return Response.failResp(state);
	}

}
